package httputil

import (
	"encoding/json"
	"github.com/go-resty/resty/v2"
)

func DeleteWithClient(client *resty.Client, url string, body []byte) (statusCode int, result []byte, err error) {
	if client == nil {
		client = GetDefaultClient()
	}
	req := client.R()
	req.SetHeader("Content-Type", "application/json")
	if len(body) > 0 {
		req.SetBody(body)
	}
	res, err := req.Delete(url)
	if err != nil {
		return 400, []byte{}, err
	}
	return res.StatusCode(), res.Body(), nil
}

func Delete(url string, body []byte) (statusCode int, result []byte, err error) {
	client := GetDefaultClient()
	return DeleteWithClient(client, url, body)
}

func DeleteWithResult(url string, body []byte, result interface{}) (statusCode int, err error) {
	statusCode, res, err := Delete(url, body)
	if err != nil {
		return statusCode, err
	}
	if len(res) == 0 || result == nil {
		return statusCode, nil
	}
	err = json.Unmarshal(res, result)
	if err != nil {
		return statusCode, err
	}
	return statusCode, nil
}
